package game

import (
	"encoding/json"
	"fmt"
	"gosum/server/src/csv"
	"io/ioutil"
	"os"
)

type Weapon struct {
	WeaponId int
	KeyId    int

	Level       int
	Star        int
	Exp         int
	StarLevel   int
	RefineLevel int
	RoleId      int
}

type ModWeapon struct {
	WeaponInfo map[int]*Weapon
	MaxKey     int

	player *Player
	path   string
}

func (self *ModWeapon) AddItem(itemId int, num int64) {
	config := csv.GetWeaponConfig(itemId)
	if config == nil {
		fmt.Println("武器不存在")
		return
	}

	if len(self.WeaponInfo)+int(num) > csv.WEAPON_MAX_COUNT {
		fmt.Println("超出最大值")
		return
	}

	for i := int64(0); i < num; i++ {
		weapon := new(Weapon)
		weapon.WeaponId = itemId
		self.MaxKey++
		weapon.KeyId = self.MaxKey
		//以weapon.keyid为键值将weapon传入WeaponInfo
		self.WeaponInfo[weapon.KeyId] = weapon
		fmt.Println("获得武器: ", csv.GetItemName(itemId), "武器编号: ", weapon.KeyId)
	}
}

func (self *ModWeapon) UpWeapon(itemId int, player *Player) {
	weapon := self.WeaponInfo[itemId]
	if weapon == nil {
		return
	}
	weaponConfig := csv.GetWeaponConfig(weapon.WeaponId)
	if weaponConfig == nil {
		return
	}
	weapon.Exp += 5000

	for {
		//取下一级配置
		nextLevelConfig := csv.GetWeaponLevelConfig(weaponConfig.Star, weapon.Level+1)

		if nextLevelConfig == nil {
			fmt.Println("返还武器经验:", weapon.Exp)
			weapon.Exp = 0
			break
		}

		//武器星级不够 不能升级
		if weapon.StarLevel < nextLevelConfig.NeedStarLevel {
			fmt.Println("返还武器经验:", weapon.Exp)
			weapon.Exp = 0
			break
		}

		//经验不够 不能升级
		if weapon.Exp < nextLevelConfig.NeedExp {
			break
		}

		weapon.Level++
		weapon.Exp -= nextLevelConfig.NeedExp
	}
	weapon.ShowInfo()
}

func (self *ModWeapon) UpWeaponStar(itemId int, player *Player) {
	weapon := self.WeaponInfo[itemId]
	if weapon == nil {
		return
	}
	weaponConfig := csv.GetWeaponConfig(weapon.WeaponId)
	if weaponConfig == nil {
		return
	}
	nextStarConfig := csv.GetWeaponStarConfig(weaponConfig.Star, weapon.Level+1)
	if nextStarConfig == nil {
		return
	}

	if weapon.Level < nextStarConfig.Level {
		fmt.Println("武器等级不够，无法突破")
		return
	}

	//todo 突破需要材料
	//验证物品数量、扣除

	weapon.StarLevel++
	weapon.ShowInfo()
}

func (self *ModWeapon) WeaponUpRefine(keyId int, targetKeyId int, player *Player) {

	weapon := self.WeaponInfo[keyId]
	if weapon == nil {
		return
	}

	weaponTarget := self.WeaponInfo[targetKeyId]
	if weaponTarget == nil {
		return
	}

	if keyId == targetKeyId {
		fmt.Println("必须选择另一个相同的武器精炼")
		return
	}

	if weapon.WeaponId != weaponTarget.WeaponId {
		fmt.Println("错误的材料")
		return
	}

	if weapon.RefineLevel >= csv.WEAPON_MAX_REFINE {
		fmt.Println("超过最大精炼等级")
		return
	}

	weapon.RefineLevel++
	delete(self.WeaponInfo, targetKeyId)
	weapon.ShowInfo()
}

func (w *Weapon) ShowInfo() {
	fmt.Println(fmt.Sprintf("key:%d,Id:%d", w.KeyId, w.WeaponId))
	fmt.Println(fmt.Sprintf("当前等级:%d,当前经验:%d,当前突破等级:%d,当前精炼等级:%d",
		w.Level, w.Exp, w.StarLevel, w.RefineLevel))
}

func (self *ModWeapon) SaveData() {
	content, err := json.Marshal(self)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(self.path, content, os.ModePerm)
	if err != nil {
		return
	}
}

func (self *ModWeapon) LoadData(player *Player) {

	self.player = player
	self.path = self.player.localPath + "/weapon.json"

	configFile, err := ioutil.ReadFile(self.path)
	if err != nil {
		self.InitData()
		return
	}
	err = json.Unmarshal(configFile, &self)
	if err != nil {
		self.InitData()
		return
	}

	if self.WeaponInfo == nil {
		self.WeaponInfo = make(map[int]*Weapon)
	}
	return
}

func (self *ModWeapon) InitData() {
	if self.WeaponInfo == nil {
		self.WeaponInfo = make(map[int]*Weapon)
	}
}
